//
//  ZhuCeViewController.m
//  豆瓣
//
//  Created by dllo on 15/11/2.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "ZhuCeViewController.h"
#import "DataBaseSingleton.h"
#import "User.h"
@interface ZhuCeViewController ()

@property (nonatomic, retain) UILabel *nameLabel;
@property (nonatomic, retain) UILabel *passwordLabel;
@property (nonatomic, retain) UILabel *confirmPassword;
@property (nonatomic, retain) UILabel *telephoneNum;
@property (nonatomic, retain) UILabel *email;

@end

@implementation ZhuCeViewController

- (void)dealloc
{
    [_email release];
    [_teleNumFd release];
    [_confirmFd release];
    [_passwordFd release];
    [_nameFd release];
    [_nameLabel release];
    [_passwordLabel release];
    [_confirmPassword release];
    [_telephoneNum release];
    [_email release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    //注册用户
    self.nameLabel = [[UILabel alloc] initWithFrame: CGRectMake(50, 100, 80, 30)];
    self.nameLabel.text = @"用户名:";
    [self.view addSubview:self.nameLabel];
    [_nameLabel release];
    
    //注册密码
    self.passwordLabel = [[UILabel alloc] initWithFrame: CGRectMake(50, 150, 80, 30)];
    self.passwordLabel.text = @"密码:";
    [self.view addSubview:self.passwordLabel];
    [_passwordLabel release];
    
    //确认密码
    self.confirmPassword = [[UILabel alloc] initWithFrame: CGRectMake(50, 200, 80, 30)];
    self.confirmPassword.text = @"确认密码:";
    [self.view addSubview:self.confirmPassword];
    [_confirmPassword release];
    
    //手机号
    self.telephoneNum = [[UILabel alloc] initWithFrame: CGRectMake(50, 250, 80, 30)];
    self.telephoneNum.text = @"手机号:";
    [self.view addSubview:self.telephoneNum];
    [_telephoneNum release];
    
    
    //注册 邮箱号
    self.email = [[UILabel alloc] initWithFrame: CGRectMake(50, 300, 80, 30)];
    self.email.text = @"邮箱:";
    [self.view addSubview:self.email];
    [_email release];
    
    
    
    //请输入用户名
    self.nameFd = [[UITextField alloc] initWithFrame:CGRectMake(150, 100, 150, 30)];
    self.nameFd.borderStyle = 3;
    self.nameFd.placeholder = @"请输入用户名";
    [self.view addSubview:self.nameFd];
    [_nameFd release];
    self.nameFd.clearButtonMode = UITextFieldViewModeAlways;
    
    //输入密码
    self.passwordFd = [[UITextField alloc] initWithFrame:CGRectMake(150, 150, 150, 30)];
    self.passwordFd.borderStyle = 3;
    self.passwordFd.placeholder = @"请输入密码";
    self.passwordFd.secureTextEntry = YES;
    [self.view addSubview:self.passwordFd];
    [_passwordFd release];
    self.passwordFd.clearButtonMode = UITextFieldViewModeAlways;
    
    //输入密码 确认密码
    self.confirmFd = [[UITextField alloc] initWithFrame:CGRectMake(150, 200, 150, 30)];
    self.confirmFd.borderStyle = 3;
    self.confirmFd.placeholder = @"请再次输入密码";
    self.confirmFd.secureTextEntry = YES;
    [self.view addSubview:self.confirmFd];
    [_confirmFd release];
    self.confirmFd.clearButtonMode = UITextFieldViewModeAlways;
    
    //输入手机号
    self.teleNumFd = [[UITextField alloc] initWithFrame:CGRectMake(150, 250, 150, 30)];
    self.teleNumFd.borderStyle = 3;
    self.teleNumFd.placeholder = @"请输入手机号";
    [self.view addSubview:self.teleNumFd];
    self.teleNumFd.keyboardType = UIKeyboardTypePhonePad;
    [_teleNumFd release];
    self.teleNumFd.clearButtonMode = UITextFieldViewModeAlways;
    
    
    //输入邮箱
    self.emailFd = [[UITextField alloc] initWithFrame:CGRectMake(150, 300, 150, 30)];
    self.emailFd.borderStyle = 3;
    self.emailFd.placeholder = @"请输入邮箱";
    [self.view addSubview:self.emailFd];
    [_emailFd release];
    self.emailFd.clearButtonMode = UITextFieldViewModeAlways;
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(70, 350, 100, 30);
    [button setTitle:@"注册" forState:UIControlStateNormal];
    button.backgroundColor = [UIColor yellowColor];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    button1.frame = CGRectMake(180, 350, 100, 30);
    [button1 setTitle:@"取消" forState:UIControlStateNormal];
    button1.backgroundColor = [UIColor redColor];
    [button1 addTarget:self action:@selector(releaseAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button1];
    
}

- (void)buttonAction:(UIButton *)btn
{
    User *user = [[User alloc] init];
    user.user = self.nameFd.text;
    user.passwd = self.passwordFd.text;
    
    NSMutableArray *array = [[DataBaseSingleton shareDataBase] selectUserName];
    NSLog(@"%@", array);
    
    if ([array containsObject:user.user]) {
        
        UIAlertController *alertCon = [UIAlertController alertControllerWithTitle:@"提示" message:@"用户名已存在" preferredStyle:UIAlertControllerStyleAlert];
        
        [self presentViewController:alertCon animated:YES completion:^{
            
        }];
        UIAlertAction *alertActCn = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
            
            
        }];
        [alertCon addAction:alertActCn];
        
    }else{
        [self.navigationController popToRootViewControllerAnimated:YES];
        [[DataBaseSingleton shareDataBase] insertUserList:user];
        
    }
    
}

- (void)releaseAction:(UIButton *)bbb
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.nameFd resignFirstResponder];
    [self.passwordFd resignFirstResponder];
    [self.confirmFd resignFirstResponder];
    [self.teleNumFd resignFirstResponder];
    [self.emailFd resignFirstResponder];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
